<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<script type="text/javascript">
<!--

$(function() {  
	$('#search_checkall').click(function () {
		$('input[name="search_check"]').attr("checked",this.checked);  
	});
}); 

function addQuestion() {
	var qIDs = getCheckedVal();
	if (qIDs.length == 0) {
		showError("没有选择试题");
	}
	else {
		$("#question_IDs").attr("value",qIDs);
		$("#add_form").submit();
	}
}

/**
 * 取得所有选中的checkbox的值
 */
function getCheckedVal() {
	var checkedVal = [];
	$('input[name="search_check"]:checked').each(function()
			{
				checkedVal.push($(this).val());
			});
	return checkedVal.join(',');
}

/**
 * ajax方式查询试题
 */
function queryQuestion() {
	var searchText = $("#searchInput").attr("value");
	if (searchText == null || searchText == "") {
		showError("请输入查询参数");
		return;
	}

	$.ajax({
		url : '<%=basePath%>backend/backend.do?action=ajaxQuestion',
		data : {
			flag:"0",
			serial : searchText
		},
		type : 'post',
		cache : false,
		dataType : 'json',
		success : function(data) {
			if (data.Result == 0) {
				if (data.questionsSize == 0) {
					showError("没有符合条件的数据");
				} else {
					$.each(data.questions, function(i, item) {
						$('#search_table > tbody:last').append(genTableRowStr(item.id, item.questionSummary));
					});
				}
			} else {
				showError("没有符合条件的数据");
			}
		}
	});
}

/**
 * 生成Table的Row
 */
function genTableRowStr(qID, qSummary) {
	var arrStrData = [];
	arrStrData.push('<tr>');
	arrStrData.push('	<td class="checkbox"><input type="checkbox" id="search_check" name="search_check" value="' + qID + '"/></td>');
	arrStrData.push('	<td>' + qSummary + '</td>');
	arrStrData.push('</tr>');
	return arrStrData.join('');
}

function showError(message) 
{
	$().Message({type:'error', time:2000,text:message, target:"#errorDiv", click:true});	
}

//-->
</script>
<form id='add_form' name='add_form' action='<%=basePath%>backend/backend.do?action=addPaperQuestion' method='POST'>
<input type="hidden" id="paper_ID" name="paper_ID" value="${paper.id}" />
<input type="hidden" id="question_IDs" name="question_IDs" value="" />
</form>
<div id="submenu">
	<div class="modules_left">
		<div class="module buttons">
			<a href="" class="dropdown_button"><small class="icon plus"></small><span>从题库添加</span></a>
			<div class="dropdown">
				<div class="arrow"></div>
				<div class="content">
					<form>
						<p>
							<label for="name">查找试题</label>
							<input type="text" class="text w_15" value="" name="searchInput" id="searchInput" />
							<a href="javascript:queryQuestion();" class="button green right"><small class="icon check"></small><span>查询</span></a>
						</p>
						<p>
							<div id="errorDiv">
							</div>
						</p>
						<p>
							<div style="overflow:scroll;height:300px;width:100%;overflow:auto">
								<table id="search_table" style="overflow:hidden; height=500px;">
									<tr>
										<th class="checkbox"><input type="checkbox" id="search_checkall" name="search_checkall" /></th>
										<th>试题名称</th>
									</tr>
								</table>
							</div>
						</p>
					</form>
					<a href="javascript:addQuestion();" class="button green right"><small class="icon check"></small><span>Save</span></a>
					<a class="button red mr right close"><small class="icon cross"></small><span>Close</span></a>
					<div class="clear"></div>
				</div>
			</div>
		</div>
	</div>
	<div class="title">
		<c:out value="${paper.paperTitle}" />
	</div>
</div>
<div class="clear"></div>
<div class="col w10 last">
	<div class="content">
		<table style="overflow:hidden; ">
			<tr>
				<th width="85%">试题名称</th>
				<th width="15%">操作</th>
			</tr>
			<c:forEach var="question" items="${questionList}">
				<tr id="id_1">
					<td><c:out value="${question.questionSummary}" />...</td>
					<td>
						<a href="<%=basePath%>backend/backend.do?action=removePaperQuestion&paperID=${paper.id}&questionID=${question.id}"><span>移除</span></a>
					</td>
				</tr>
			</c:forEach>
		</table>
	</div>							
</div>
<div class="clear"></div>
